﻿using System;
using System.Collections.Generic;
using System.Linq;
using Learun.Util;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-02-24 11:12
    /// 描 述：对象类型属性表
    /// </summary>
    public class ec_objecttypepBLL : ec_objecttypepIBLL
    {
        private ec_objecttypepService ec_objecttypepService = new ec_objecttypepService();

        #region 获取数据
        /// <summary>
        /// 获取对象类型属性表数据。要注意，如果objectTypeID是“”，则是查询全部的。
        /// OPC特殊处理。
        /// <summary>
        /// <param name="objectTypeID">对象类型ID。多个对象类型以逗号风格。如果是“”，则是查询全部的</param>
        /// <param name="ProjectId">项目ID</param>
        /// <param name="isLoadExtraData">是否加载额外的数据</param>
        /// <returns></returns>
        public IEnumerable<ec_objecttypepEntity> GetObjectTypePById(string objectTypeID, string ProjectId, bool isLoadExtraData = false)
        {
            try
            {

                if (objectTypeID == "OPC")
                {
                    var res = new ec_OPCBLL().GetOPCPropertyDef();

                    return res;
                }
                else
                {
                    var RES = new List<ec_objecttypepEntity>();
                    if (objectTypeID.Contains(","))
                    {
                        if (objectTypeID.Split(',').Any(X => X == "OPC"))
                        {
                            var res = new ec_OPCBLL().GetOPCPropertyDef();
                            RES.AddRange(res);
                        }
                    }
                    var res2 = ec_objecttypepService.GetObjectTypePById(objectTypeID, ProjectId, isLoadExtraData);
                    RES.AddRange(res2);
                    return RES;
                }

            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 获取属性反向关联的对象类型表数据
        /// <summary>
        /// <param name="propertyID">属性ID</param>
        /// <param name="ProjectId">项目ID</param>
        /// <returns></returns>
        public IEnumerable<ec_objecttypepEntity> GetPropertyByObjectId(string propertyID, string ProjectId)
        {
            try
            {
                return ec_objecttypepService.GetPropertyByObjectId(propertyID, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue, string ProjectId)
        {
            try
            {
                ec_objecttypepService.DeleteEntity(keyValue, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 保存对象类型属性表数据（新增）
        /// <param name="objectTypeID">对象类型ID</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string objectTypeID, List<ec_objecttypepEntity> entityList, string ProjectId)
        {
            try
            {
                ec_objecttypepService.SaveEntity(objectTypeID, entityList, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 保存属性反向关联的对象类型数据（新增）
        /// <param name="propertyID">属性ID</param>
        /// <summary>
        /// <returns></returns>
        public void SavePropertyO(string propertyID, string objectTypeIDs, string ProjectId)
        {
            try
            {
                var listObjectTypeID = objectTypeIDs.Split(',').ToList();
                List<ec_objecttypepEntity> entityList = new List<ec_objecttypepEntity>();
                foreach (var item in listObjectTypeID)
                {
                    ec_objecttypepEntity model = new ec_objecttypepEntity();
                    model.ObjectTypePID = Guid.NewGuid().ToString();
                    model.ObjectTypeID = item;
                    model.PropertyID = propertyID;
                    entityList.Add(model);
                }
                ec_objecttypepService.SavePropertyO(propertyID, entityList, ProjectId);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        #endregion

    }
}
